package nz.co.listit.TaxiValet;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class ListItC2dmReceiver extends BroadcastReceiver {
	private static String KEY = "c2dmPref";
	private static String REGISTRATION_KEY = "registrationKey";

	// C2DM Intent actions
	public static String INTENT_C2DM_FMT = "nz.co.listit.TaxiValet.C2DM.";
	public static String INTENT_C2DM_REGISTERED = INTENT_C2DM_FMT
			+ "REGISTERED";
	public static String INTENT_C2DM_SERVICE_NOT_AVAILABLE = INTENT_C2DM_FMT
			+ "SERVICE_NOT_AVAILABLE";
	public static String INTENT_C2DM_ACCOUNT_MISSING = INTENT_C2DM_FMT
			+ "ACCOUNT_MISSING";
	public static String INTENT_C2DM_AUTHENTICATION_FAILED = INTENT_C2DM_FMT
			+ "AUTHENTICATION_FAILED";
	public static String INTENT_C2DM_TOO_MANY_REGISTRATIONS = INTENT_C2DM_FMT
			+ "TOO_MANY_REGISTRATIONS";
	public static String INTENT_C2DM_INVALID_SENDER = INTENT_C2DM_FMT
			+ "INVALID_SENDER";
	public static String INTENT_C2DM_PHONE_REGISTRATION_ERROR = INTENT_C2DM_FMT
			+ "PHONE_REGISTRATION_ERROR";
	public static String INTENT_C2DM_UNREGISTERED = INTENT_C2DM_FMT
			+ "UNREGISTERED";
	public static String INTENT_C2DM_UKNOWN_STATE = INTENT_C2DM_FMT
			+ "UNKNOWN_STATE";

	// send this when we have a new job request
	public static String INTENT_C2DM_NEW_JOB = INTENT_C2DM_FMT + "NEW_JOB";
	public static String INTENT_C2DM_SERVER_MESSAGE = INTENT_C2DM_FMT
			+ "SERVER_MESSAGE";

	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(
				"com.google.android.c2dm.intent.REGISTRATION")) {
			handleRegistration(context, intent);
		} else if (intent.getAction().equals(
				"com.google.android.c2dm.intent.RECEIVE")) {
			handleMessage(context, intent);
		}
	}

	private void handleRegistration(Context context, Intent intent) {
		String registration = intent.getStringExtra("registration_id");
		if (intent.getStringExtra("error") != null) {
			// Registration failed, should try again later.
			Log.d("c2dm", "registration failed");

			String error = intent.getStringExtra("error");
			if (error.equals("SERVICE_NOT_AVAILABLE")) {
				Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
			} else if (error.equals("ACCOUNT_MISSING")) {
				Log.d("c2dm", "ACCOUNT_MISSING");
			} else if (error.equals("AUTHENTICATION_FAILED")) {
				Log.d("c2dm", "AUTHENTICATION_FAILED");
			} else if (error.equals("TOO_MANY_REGISTRATIONS")) {
				Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
			} else if (error.equals("INVALID_SENDER")) {
				Log.d("c2dm", "INVALID_SENDER");
			} else if (error.equals("PHONE_REGISTRATION_ERROR")) {
				Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
			} else {
				Log.d("c2dm", error);
			}

			// inform our UI that something went pop!
			context.sendBroadcast(new Intent(INTENT_C2DM_FMT + error));
		} else if (intent.getStringExtra("unregistered") != null) {
			// un-registration done, new messages from the authorized sender
			// will be rejected
			Log.d("c2dm", "unregistered");

			context.sendBroadcast(new Intent(INTENT_C2DM_UNREGISTERED));
		} else if (registration != null) {
			Log.d("c2dm", registration);
			Editor editor = context.getSharedPreferences(KEY,
					Context.MODE_PRIVATE).edit();
			editor.putString(REGISTRATION_KEY, registration);
			editor.commit();

			// notify our activity that we are now registered
			context.sendBroadcast(new Intent(INTENT_C2DM_REGISTERED));
		} else {
			// unknown return state
			context.sendBroadcast(new Intent(INTENT_C2DM_UKNOWN_STATE));
		}
	}

	private void handleMessage(Context context, Intent intent) {
		if (intent.hasExtra("msg")) {
			Intent c2dmIntent = new Intent(INTENT_C2DM_SERVER_MESSAGE);
			
			c2dmIntent.putExtra("msg", intent.getStringExtra("msg"));
			// notify our activity that we are now registered
			context.sendBroadcast(c2dmIntent);
		} else {
			// notify our activity that we are now registered
			context.sendBroadcast(new Intent(INTENT_C2DM_NEW_JOB));
		}
	}

	public static String getC2DMRegistrationKey(Context context) {

		return context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.getString(REGISTRATION_KEY, "ABC123");
	}
}
